package yxy.game.pm2.command.infinite;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.infinitefirstreward;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.module.RedisLock;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.InfiniteMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.infinite.InfiniteModule;

import java.util.Collection;

/** 神将试炼 神将试炼领取首通奖励 */
@MessageType(proto = Command.InfiniteFirstReward, classtype = InfiniteMessage.InfiniteFirstRewardRequest.class)
public class CMD_InfiniteFirstReward extends Command<InfiniteMessage.InfiniteFirstRewardRequest> {

	@Override
	public void execute(MessageSource messageSource, InfiniteMessage.InfiniteFirstRewardRequest request) {
		String playerCode = messageSource.getTag();
		int stage = request.getStage();

		InfiniteMessage.InfiniteFirstRewardResponse.Builder response = InfiniteMessage.InfiniteFirstRewardResponse.newBuilder();

		RedisLock lock = InfiniteModule.lock(playerCode);
		try {
			if (lock.tryLock()) {
				InfiniteModule infiniteModule = InfiniteModule.get(playerCode);
				if (infiniteModule == null) {
					send(messageSource, response, Result.ModuleNotOpen);// 模块没开启
					return;
				}
				infinitefirstreward data = DataCenter.getData(stage, infinitefirstreward.class);
				if (data == null) {
					send(messageSource, response, Result.InfiniteBoxHasRewarded);// 宝箱数据不存在
					return;
				}

				if (infiniteModule.getBox() >= data.getId()) {
					send(messageSource, response, Result.InfiniteBoxHasRewarded);// 宝箱已领过
					return;
				}

				if(infiniteModule.getBest() < data.getId()){
					send(messageSource, response, Result.InfiniteBoxHasNotRewarded);// 宝箱不能领取
					return;
				}

				Collection<Currency> rewards = Currency.create(data.getFirsttype(), data.getFirstid(), data.getFirstnum());
				new CurrencyHandle().addCurrency(playerCode, CCReason.神将试炼, rewards);
				infiniteModule.setBox(data.getId());
				infiniteModule.set();

				response.addAllBox(infiniteModule.rewardBox());
				response.addAllRewards(new CurrencyHandle().packItems(rewards));

				Iterable<? extends Integer> boxes = infiniteModule.rewardBox();
				int nextBox = infiniteModule.getBest() % 5 == 0 ? infiniteModule.getBest() + 5 : infiniteModule.getBest() - (infiniteModule.getBest() % 5) + 5;
				if (!boxes.iterator().hasNext()){
					if (infiniteModule.getBest() == 0){
						response.setStage(5);
					}else if (nextBox >= infiniteModule.getBest()){
						response.setStage(nextBox - infiniteModule.getStart() + 1);
					}else if (nextBox < infiniteModule.getBest()){
						response.setStage(0);
					}
				}
				for (Integer box : boxes) {
					if (box > infiniteModule.getBest()){
						response.setStage(box - infiniteModule.startStage() + 1);  //剩余关卡数
						break;
					}else if (box < infiniteModule.getBest()){
						response.setStage(0); //可领取
						break;
					}
				}

				send(messageSource, response, Result.Success);
			}
		} catch (Exception e) {
			e.printStackTrace();
			send(messageSource, response, Result.DataError);
		} finally {
			lock.unlock();
		}
	}

	private void send(MessageSource messageSource, InfiniteMessage.InfiniteFirstRewardResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}